package com.woniu.fly.fairmall.wx.web;

import com.woniu.fly.fairmall.core.util.ResponseUtil;
import com.woniu.fly.fairmall.db.entity.QuestInfo;
import com.woniu.fly.fairmall.db.entity.pojo.QuestVo;
import com.woniu.fly.fairmall.db.service.QuestInfoService;
import com.woniu.fly.fairmall.db.service.QuestMainService;
import com.woniu.fly.fairmall.wx.annotation.LoginUser;
import com.woniu.fly.fairmall.wx.service.QuestUserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 问卷服务
 */
@RestController
@RequestMapping("/wx/quest")
public class WxQuestController {
    private final Log logger = LogFactory.getLog(WxQuestController.class);

    @Resource
    private QuestMainService questMainService;

    @Resource
    private QuestInfoService questInfoService;

    @Resource
    private QuestUserService questUserService;

    /**
     * @Discription 获取问卷信息
     * @author YangChunhai
     * @created 2020/4/22 9:30
     */
    @GetMapping("/list")
    public Object list(@LoginUser Long userId) {
        List<Map<String, Object>> questMains = questMainService.queryAllQuest(userId);
        return ResponseUtil.ok(questMains);
    }

    /**
     * @Discription 获取问卷信息
     * @author YangChunhai
     * @created 2020/4/22 9:30
     */
    @GetMapping("/detail")
    public Object detail(@LoginUser Long userId, @NotNull Long id) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        // 查询问卷是否已填写
        List<QuestInfo> questInfos = questInfoService.queryInfoByUserId(userId, id);
        if (!CollectionUtils.isEmpty(questInfos)) {
            return ResponseUtil.fail(507,"这个问卷您已填写咯");
        }
        QuestVo questVo = questMainService.queryQuestDetail(id);
        return ResponseUtil.ok(questVo);
    }

    /**
     * @Discription 获取问卷信息
     * @author YangChunhai
     * @created 2020/4/22 9:30
     */
    @PostMapping("/user")
    public Object user(@LoginUser Long userId, @RequestBody String body) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        return questUserService.submit(userId, body);
    }

}
